/**
 * SwitchImg
 * 简单切换图片类
 * @author yulipu
 */
function SwitchImg() {
	this.imgObj = null;
	this.titleObj = null;
	this.handlers = [];
	this.idx = 0;  // 当前图片索引
	this.t = 6000;  // 6秒后自动执行
	this.closeClass = 'y-dot';
	this.openClass = 'y-dot y-dot-on';
}
SwitchImg.prototype = {
	constructor : SwitchImg
	,closeAllBg : function() {
		var len = this.handlers.length;
		for(var i=0; i<len; i++) {
			this.handlers[i].className = this.closeClass;
		}
	}
	,autoExecute : function() {
		var _self = this;
		if(this.idx == this.handlers.length) {this.idx = 0;}
		
		setTimeout(function(){
			_self.closeAllBg();
			_self.firstBoot(_self.idx);
			
		}, _self.t);
	}
	,firstBoot : function(x) {
		// 当前背景打开
		this.handlers[x].className = this.openClass;
		// 更改图片
		this.imgObj.src = this.handlers[x].getAttribute('data-img');
		// 更改标题
		this.titleObj.innerHTML = this.handlers[x].getAttribute('data-title');
		// 连接
		if(null != this.handlers[x].getAttribute('data-link')) {
			this.imgObj.parentNode.href = this.handlers[x].getAttribute('data-link');
		}
		
		this.idx = ++x;
		this.autoExecute();
	}
	,start : function() {
		var _self = this;
		var len = _self.handlers.length;
		// 第一次加载处理
		_self.firstBoot(0);
		
		for(var i=0; i<len; i++) {
			_self.handlers[i].onmouseover = function(){
				// 关闭所有背景
				_self.closeAllBg();
				// 当前背景打开
				this.className = _self.openClass;
				// 更改图片
				_self.imgObj.src = this.getAttribute('data-img');
				// 更改标题
				_self.titleObj.innerHTML = this.getAttribute('data-title');
				// 连接
				if(null != this.getAttribute('data-link')) {
					_self.imgObj.parentNode.href = this.getAttribute('data-link');
				}
			};
		}
	}
	,init : function(imgObj, titleObj, handlers) {
		this.imgObj = imgObj;
		this.titleObj = titleObj;
		this.handlers = handlers;
		
		this.start();
	}
};
